#include "fifo.hpp"


int main()
{
    //1、创建命名管道
    Fifo fifo(PATH);

    //2、以读的方式打开
    //如果写端没有打开，读端就会阻塞到open，直到写端打开，open才会返回
    int rfd = open(PATH, O_RDONLY);
    if(rfd < 0)
        cerr << "open-readed failed, errno code: " << errno << ", error message: " << strerror(errno) << endl;
    else
        cout << "open-readed success!" << endl;

    //3、一直读取
    char buf[1024];
    while(true)
    {
        int n = read(rfd, buf, sizeof(buf));
        if(n > 0)
        {
            cout << "client says: " << buf << endl;
        }
        else if(n == 0)
        {
            cout << "serve port quit" << endl;
            break;
        }
        else
        {
            cout << "read failed" << endl;
            break;
        }
    }

    //4、关闭读端
    //close(rfd);

    return 0;
}